import JsonP from 'jsonp';
import axios from 'axios';
import { Modal } from 'antd';
import Utils from './../utils/utils';

export default class Axios {
	
	static requestList(_this, url, params, isMock){
//		var data = {
//			params: params
//		}
		this.ajax({
			url,
			params,
			isMock
		}).then((data)=>{
			if(data && data.result){
				_this.setState({
					list: data.result.list.map((item, index) => {
						item.key = index;
						return item;
					}),
					pagination: Utils.pagination(data,(current)=>{
						_this.params.page = current;
						_this.requestList();
					})
				})
			}
		})
	}
	
    static jsonp(options){
        return new Promise((resolve,reject)=>{
            JsonP(options.url,{param:'callback'},function(err,res){
				if(res.status === "1"){
					resolve(res)
				}else{
					reject(res.message)
				}
            })
        })
    }
	
	static ajax(options){
		let loading;
		if(options.data && options.data.isShowLoading !== false){
			loading = document.getElementById("ajaxLoading");
			loading.style.display = 'block';
		}
		let baseApi = "";
		if(options.isMock){
			baseApi = "";
		}else{
			baseApi = "192.168.188.172";
		}
		
		return new Promise((resolve, reject)=>{
			axios({
				url: options.url,
				method: 'get',
				baseURL: baseApi,
				timeout: 5000,
				params: (options.data && options.data.params) || ""
			}).then((response)=>{
				setTimeout(function(){
					if(options.data && options.data.isShowLoading !== false){
						loading = document.getElementById("ajaxLoading");
						loading.style.display = 'none';
					}
				},1000)
				if(response.status === 200){
					let res = response.data;
					if(res.code === 0){
						resolve(res);
					}else{
						Modal.info({
							title: "提示",
							content: res.message
						})
					}
				}else{
					reject(response.data);
				}
			})
		})
	}
}